<template>
	<view class="page tn-safe-area-inset-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
		<view class="top-bg"></view>
		<dawnBar title="挂号医生"></dawnBar>
		<view class="page-container" style="padding-bottom: 140rpx;">
			<!-- <pBox @search="getData()" :search="true"></pBox> -->
			<view>
				<view v-if="date_items.length > 0" class="tn-bg-white dawn-radius dawn-shadow">
					<tn-scroll-list :indicator="false">
						<view class="tn-flex tn-margin-top-sm tn-padding-left-xs">
							<block v-for="(item,index) in date_items" :key="index">
								<view class="tn-padding-right-xs" @click="getData(item)">
									<view class="dawn-radius tn-padding-sm tn-text-ellipsis tn-text-center"
										:class="queryData.searchDate === item.dateStr?'tn-bg-dawn':'tn-bg-dawn-gray'">
										<view>
											{{item.showWeek}}
										</view>
										<view class="tn-margin-top-ss" style="height: 18px;">
											{{item.showDate}}
										</view>
									</view>
								</view>
							</block>
						</view>
					</tn-scroll-list>
				</view>

				<!-- 标签内容 -->
				<view class="list tn-margin-top-sm">
					<view class="list-item" v-for="(item,index) in list" :key="index">
						<view @click="onDoctor(item)"
							class="list-head tn-flex tn-flex-direction-row tn-flex-col-center tn-flex-row-between">
							<view class="tn-flex tn-flex-direction-row tn-flex-col-center tn-flex-row-between">
								<text
									class="dawn-text-md tn-text-bold tn-color-dawn tn-padding-right-xs">{{item.doctorName || '' }}</text>
								<tn-tag class="dawn-tag-sm dawn-tag-sm__3">{{item.timeName || '' }}</tn-tag>
								<text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text>
								<!-- <tn-tag class="dawn-tag-sm dawn-tag-sm__1">{{item.scheduleTime || '' }}</tn-tag>
								<text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text> -->
								<tn-tag class="dawn-tag-sm dawn-tag-sm__2">{{item.scheduleTime || '' }}</tn-tag>
							</view>
							<view>
								<view class="justify-content-item ">
									<tn-tag backgroundColor="tn-main-gradient-dawn" fontColor="#FFFFFF"
										shape="circleLeft" width="auto">
										{{ item.doctorLevel || item.regType}}
									</tn-tag>
								</view>
							</view>
						</view>
						<view class="list-content tn-flex tn-flex-col-center tn-flex-row-between">
							<view class="tn-flex tn-flex-col-center tn-flex-row-left">
								<view class="">
									<image v-if="item.cms && item.cms.headImageUrl" mode="widthFix" class="doctor-image"
										:src="item.cms.headImageUrl">
										<image v-else mode="widthFix" class="doctor-image"
											src="../../static/images/icon_yygh.png">
											<view v-if="item.cms && item.cms.publish === 0" class="tn-text-center" @click="showCms(item)">
												<text class="dawn-text-ss ">简介</text>
												<text class="dawn-text-ss tn-icon-right"></text>
											</view>
								</view>
								<view class="tn-padding-left-ss" @click="onDoctor(item)">
									<view class="">
										<text class="dawn-text-sm tn-margin-right-ss">科室</text>
										<text class="dawn-text-sm tn-text-bold">{{ item.departmentName }}</text>
									</view>
									<!-- <view class="">
										<text class="dawn-text-sm tn-margin-right-ss">类型</text>
										<text class="dawn-text-sm tn-text-bold">{{ item.regType }}</text>
									</view> -->
									<view>
										<text class="dawn-text-sm tn-margin-right-ss">项目</text>
										<text class="dawn-text-sm tn-text-bold">{{ item.projectName }}</text>
									</view>
									<view class="tn-flex tn-flex-col-center">
										<block v-for="(fee,index1) in item.feeList" :key="index1">
											<view v-if="fee.SSJE > 0">
												<text class="dawn-text-sm dawn-color-grey tn-margin-right-ss">{{ fee.MC }}</text>
												<text class="dawn-text-sm tn-text-bold ">{{ float1(fee.SSJE) }}</text>
												<text v-if="index1 < (item.feeList.length - 1)" class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text>
											</view>
										</block>
									</view>
									<view class="news-desc" v-if="item.cms && item.cms.tags">
										<text class="dawn-text-sm tn-margin-right-ss tn-color-black">擅长</text>
										<text class="dawn-text-ss tn-color-gray">
											{{ item.cms.tags }}
										</text>
									</view>
								</view>
							</view>
							<view class="tn-flex tn-flex-nowrap  tn-flex-col-center tn-flex-row-left"
								@click="onDoctor(item)">
								<view class="justify-content-item tn-text-right">
									<text class="tn-icon-right tn-color-gray"></text>
								</view>
							</view>
						</view>

						<view class="dawn-line tn-margin-top-sm"></view>
						<view @click="onDoctor(item)"
							class=" tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-between">
							<view class="justify-content-item tn-text-df">
								<text class="tn-icon-money tn-color-black tn-margin-right-ss">金额</text>
								<text class="tn-color-red--dark dawn-text-lg tn-text-bold">{{ item.cost }} 元</text>
							</view>
							<view
								class="tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-right op-box">
								<view>
									<text class="tn-icon-success-circle-fill dawn-text-sm tn-margin-right-ss">已挂</text>
									<text class="dawn-text-lg">{{ item.regCount }}</text>
									<text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text>
									<text class="tn-icon-reduce-circle-fill dawn-text-sm tn-margin-right-ss">余号</text>
									<text
										class="tn-color-red--dark dawn-text-lg tn-text-bold">{{ item.limitCount }}</text>
								</view>
							</view>
						</view>
					</view>
				</view>

				<!-- 悬浮按钮-->
				<view class="tn-flex tn-footerfixed">
					<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
						<tn-button @click="getData()" class="dawn-btn-lg dawn-btn-lg__main">
							<text class="tn-icon-search-list-fill tn-padding-right-xs"></text>
							<text>查 询</text>
						</tn-button>
					</view>
				</view>
			</view>
			<view v-if="show">
				<dawnMsg :msg="msg"></dawnMsg>
			</view>
		</view>

		<tn-popup v-model="showPopup" mode="bottom" length="70%" :borderRadius="borderRadius">
			<view class="content">
				<view class="dawn-popup-head">
					<view class="list-icon-text">
						<view class="list__left">
							<view class="list__left__icon tn-icon-time-fill"></view>
							<view class="list__left__text">选择就诊时间段</view>
						</view>
						<view class="list__right">
							<tn-button @click=" showPopup = false" class="dawn-btn-sm dawn-btn-sm__defult">
								<text class="tn-icon-close-fill">关闭</text>
							</tn-button>
						</view>
					</view>
				</view>
				<!-- <scroll-view scroll-y="true"> -->
				<view style="padding-top: 50px;padding-bottom: 50px">
					<view v-if="!orderInfo.doctorSpan">
						<text class="tn-icon-right">暂无可选时间段，请选择其他号源</text>
					</view>
					<block v-else >
						<view class="tn-flex tn-flex-wrap dawn-text-sm tn-padding-left-ss">
							<block v-for="(item,index) in orderInfo.doctorSpan" :key="index">
								<view @click="onDoctorSpan(item)" class="tn-text-center dawn-bg tn-margin-top-sm tn-margin-left-xs dawn-radius" style="width: 23%;"
									:class="item.limitCount === '1'?'tn-main-gradient-dawn--light tn-shadow-blur':'dawn-color-grey'">
									<view class="tn-margin-top-sm tn-margin-bottom-xs"
										:class="item.limitCount === '1'?'dawn-color-dark':''">
										{{ item.scheduleTime.substring(0,5) }}-{{ item.scheduleTime.substring(9,14) }}
									</view>
									
									<view class="tn-margin-bottom-sm" v-if="item.limitCount === '1'">
										<text>可预约</text>
									</view>
									<view class="tn-margin-bottom-sm" v-else>
										<text>已预约</text>
									</view>
								</view>
							</block>
						</view>
						
					</block>
				</view>
				<!-- </scroll-view> -->
			</view>
		</tn-popup>


		<tn-popup v-model="showContent" mode="center" width="80%" :borderRadius="borderRadius">
			<view class="content">
				<tn-list-cell>
					<view class="tn-text-center">
						<text class="dawn-text-lg dawn-color">医生简介</text>
					</view>
				</tn-list-cell>
				<scroll-view scroll-y="true">
					<view class='conters'>
						<parser :html="cmsContent" ref="article"></parser>
					</view>
				</scroll-view>
				<view
					class="tn-flex tn-flex-row-center tn-flex-col-center tn-width-full tn-padding-top tn-padding-bottom">
					<tn-button @click="showContent = false" class="dawn-btn-lg dawn-btn-lg__info">
						<text class="tn-icon-check tn-padding-right-xs"></text>
						<text>关 闭</text>
					</tn-button>
				</view>
			</view>
		</tn-popup>

		<!-- loading -->
		<dawnLoading :show="loading"></dawnLoading>

		<!-- 分页 -->
		<!-- <dawn-pagination ref="pagination" :current="queryData.pageNo" :total="total" @pagination="getData" /> -->

		<!-- 悬浮按钮-->
		<NavBtn></NavBtn>
	</view>
</template>

<script>
	import page_mixin from '@/mixins/page_mixin.js'
	import dayjs from '@/plugin/dayjs/dayjs.min.js';
	import * as RegisterApi from "@/api/hospital/register.js";
	import * as api from '@/api/api.js';
	import * as config from "@/api/config.js";
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	const app = getApp();

	export default {
		name: 'order',
		mixins: [page_mixin],
		computed: mapGetters(['appConfig', 'uid', 'isLogin', 'userInfo', 'patientInfo']),
		data() {
			return {
				loading: false,
				show: false,
				msg: '',
				tabsIndex: 0,
				showContent: false,
				cmsContent: false,
				showPopup: false,
				borderRadius: 40,
				queryData: {
					pageSize: 10,
					pageNo: 1,
					patientId: '',
					patientName: '',
					departmentId: '',
					searchDate: ''
				},
				status: -1,
				orderInfo: {},
				date_items: [],
				list: [],
				doctorSpan: []
			}
		},
		onLoad(options) {
			console.info("query onLoad")
			if (!this.isLogin) {
				toLogin();
				return;
			} else {
				this.orderInfo = this.$util.getStorageData(config.STORE_KEY.REG_DEPT);
				this.getDate();
				// if (options.hasOwnProperty('params')) {
				// 	let params = JSON.parse(options.params);
				// 	this.orderInfo = params;
				// 	this.getDate();
				// }
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		methods: {
			search(result) {
				if (result.code === 0) {
					this.queryData = result.data;
					this.queryData.pageNo = 1;
					this.queryData.pageSize = this.$refs.pagination.pageSize;
					this.getDate();
				}
			},
			showCms(item) {
				this.cmsContent = item.cms.content;
				this.showContent = true;
			},
			toCreate(data) {
				this.$util.setStorageData(config.STORE_KEY.REG_SOURCE, data)
				uni.navigateTo({
					url: '/hosp/register/info'
				})
				// uni.navigateTo({
				// 	url: '/hosp/register/info?params=' + JSON.stringify(item)
				// })
			},
			onDoctorSpan(item) {
				if(item.limitCount === '1'){
					this.orderInfo.scheduleTime = item.scheduleTime;
					this.toCreate(this.orderInfo);
				}
			},
			onDoctor(item) {
				this.orderInfo = item;
				this.orderInfo.searchDate = this.queryData.searchDate;
				if (item.dayparting === '1') {
					this.showPopup = true;
				} else {
					this.toCreate(item);
				}
			},
			getDate: function() {
				let that = this;
				this.loading = true;
				this.show = false;
				this.$set(this, 'date_items', []);
				this.queryData.patientId = this.patientInfo.patientId;
				this.queryData.patientName = this.patientInfo.name;
				RegisterApi.getDate(this.queryData).then(res => {
					console.table(res);
					if (res.code === 0) {
						const l = res.data || [];
						that.$set(this, 'date_items', l);
						that.getData(l[0]);
					} else {
						this.show = true;
						this.msg = res.msg;
					}
				}).catch(err => {
					this.show = true;
				})
			},
			getData: function(date) {
				console.table(this.orderInfo);
				let that = this;
				this.loading = true;
				this.show = false;
				this.$set(this, 'list', []);
				this.queryData.patientId = this.patientInfo.patientId;
				this.queryData.patientName = this.patientInfo.name;
				this.queryData.departmentId = this.orderInfo.departmentId;

				if (date) {
					this.queryData.searchDate = date.dateStr;
				}
				console.table(this.queryData);
				RegisterApi.getDoctor(this.queryData).then(res => {
					this.loading = false;
					console.table(res);
					if (res.code === 0) {
						const l = res.data || [];

						// const array = l.filter((val) => (val.payStatus === 'payoff'));

						const array = new Array();
						l.forEach(function(item, index) {
							if (item.limitCount > 1) {
								array.push(item);
							}
						});

						that.$set(this, 'list', array);

						if (array.length == 0) {
							that.show = true;
							that.msg = '暂无挂号医生';
						}

					} else {
						this.show = true;
						this.msg = res.msg;
					}
				}).catch(err => {
					this.loading = false;
					this.show = true;
				})
			},
			formatDate: function(date) {
				return dayjs(date).format("YYYY-MM-DD");
			},
			formatDateTime: function(date) {
				return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list-box {
		padding: 0rpx 20rpx;
	}

	.list-icon-text,
	.list-image-text {
		padding: 20rpx 0rpx;
		border-bottom: 1px solid $dawn-line-color;
	}

	.list__left__text {
		color: #000;
	}

	::v-deep .tn-index-list__sidebar {
		right: 20rpx;
	}

	::v-deep .tn-index-list__sidebar__item {
		font-size: $dawn-font-size-xl;
	}

	::v-deep .tn-index-anchor {
		font-size: $dawn-font-size-md;
		width: 100%;
		box-sizing: border-box;
		padding: 10rpx 20rpx;
		color: #080808;
		font-weight: bolder;
		line-height: 1.2;
		background-color: $dawn-page-bg;
	}

	::v-deep .tn-index-anchor--active {
		color: #080808;
	}

	// ::v-deep .tn-scroll-list__scroll-view__content {
	// 	display: flex;
	// 	flex-direction: row;
	// 	justify-content: space-around;
	// }

	.doctor-image {
		// background-color: #E6E6E6;
		margin-right: 10rpx;
		width: 110rpx;
		border-radius: $dawn-border-radius;
	}
</style>